package me.zhengjie.hr.employeestate.service.impl;

import me.zhengjie.hr.employeestate.domain.EmployeeState;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.hr.employeestate.repository.EmployeeStateRepository;
import me.zhengjie.hr.employeestate.service.EmployeeStateService;
import me.zhengjie.hr.employeestate.service.dto.EmployeeStateDto;
import me.zhengjie.hr.employeestate.service.dto.EmployeeStateQueryCriteria;
import me.zhengjie.hr.employeestate.service.mapper.EmployeeStateMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @author ZGZ
* @date 2020-04-15
*/
@Service
//@CacheConfig(cacheNames = "employeeState")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class EmployeeStateServiceImpl implements EmployeeStateService {

    private final EmployeeStateRepository employeeStateRepository;

    private final EmployeeStateMapper employeeStateMapper;

    public EmployeeStateServiceImpl(EmployeeStateRepository employeeStateRepository, EmployeeStateMapper employeeStateMapper) {
        this.employeeStateRepository = employeeStateRepository;
        this.employeeStateMapper = employeeStateMapper;
    }

    @Override
    //@Cacheable
    public Map<String,Object> queryAll(EmployeeStateQueryCriteria criteria, Pageable pageable){
        Page<EmployeeState> page = employeeStateRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(employeeStateMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<EmployeeStateDto> queryAll(EmployeeStateQueryCriteria criteria){
        return employeeStateMapper.toDto(employeeStateRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public EmployeeStateDto findById(Integer id) {
        EmployeeState employeeState = employeeStateRepository.findById(id).orElseGet(EmployeeState::new);
        ValidationUtil.isNull(employeeState.getId(),"EmployeeState","id",id);
        return employeeStateMapper.toDto(employeeState);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public EmployeeStateDto create(EmployeeState resources) {
        return employeeStateMapper.toDto(employeeStateRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(EmployeeState resources) {
        EmployeeState employeeState = employeeStateRepository.findById(resources.getId()).orElseGet(EmployeeState::new);
        ValidationUtil.isNull( employeeState.getId(),"EmployeeState","id",resources.getId());
        employeeState.copy(resources);
        employeeStateRepository.save(employeeState);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            employeeStateRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<EmployeeStateDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (EmployeeStateDto employeeState : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("人员id", employeeState.getEmpId());
            map.put("地点", employeeState.getBusinessAdd());
            map.put("开始时间", employeeState.getStartTime());
            map.put("结束时间", employeeState.getEndTime());
            map.put("事由", employeeState.getReason());
            map.put("类型（出差/休假/病假/事假）", employeeState.getType());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}